import { MAP } from "../../Types";
import { breadth } from "treeverse";
import { Coord } from "../../utils/Coord";
const getAround = (x: number, y: number) => [
    [x - 1, y],
    [x + 1, y],
    [x, y + 1],
    [x, y - 1],
];
/** 查找一个洞穴所拥有的格子 */
export function GetRegionTiles(
    startX: number,
    startY: number,
    map: MAP,
    tileType: number,
    Founded: MAP
): Coord[] {
    const tiles: Coord[] = [];
    // 广度遍历这些节点形成
    breadth<Coord, void>({
        tree: new Coord(startX, startY),
        visit(node) {
            tiles.push(node);
            Founded.set(node.tileX, node.tileY, 1);
        },
        getChildren({ tileX, tileY }) {
            return getAround(tileX, tileY)
                .filter(([x, y]) => {
                    if (
                        Founded.get(x, y) === 0 &&
                        map.get(x, y)! === tileType
                    ) {
                        return true;
                    }
                })
                .map(([x, y]) => {
                    Founded.set(x, y, 1);
                    return new Coord(x, y);
                });
        },
    });
    return tiles;
}
